package com.atguigu.daijia.dispatch.xxl.handler;

import com.atguigu.daijia.dispatch.mapper.XxlJobLogMapper;
import com.atguigu.daijia.dispatch.service.NewOrderService;
import com.atguigu.daijia.model.entity.dispatch.XxlJobLog;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author 山顶洞人郑某
 * @className XxlJobHandler.java
 * @date 2024 09 11
 */
@Component
@Slf4j
public class XxlJobHandler {


    @Autowired
    private NewOrderService newOrderService;
    @Autowired
    private XxlJobLogMapper xxlJobLogMapper;
    @XxlJob("newOrderTask")  //新增订单定时任务
    public void newOrderTask() {
        XxlJobLog xxlJobLog = new XxlJobLog();
        long jobId = XxlJobHelper.getJobId();
        xxlJobLog.setJobId(jobId);
        long millis = System.currentTimeMillis();
        try {
            //执行任务
            newOrderService.excuteTask(jobId);
            xxlJobLog.setStatus(1);
        } catch (Exception e) {
            xxlJobLog.setStatus(0);
            xxlJobLog.setError(e.getMessage());
        } finally {
            long current = System.currentTimeMillis() - millis;
            xxlJobLog.setTimes((int)current); //执行时间
            //记录日志
            xxlJobLogMapper.insert(xxlJobLog);
        }
    }
}
